
*************************************************************************************************************
* This dofile produces the figures and tables in the main text of
* Love Thy (Elected) Neighbor? Residential segregation, political representation and local public goods
* by Oskari Harjunen, Tuukka Saarimaa and Janne Tukiainen
* Stata/MP 17.0
*************************************************************************************************************

*Pahts
global results "C:\Users\saarimt1\Dropbox\Love thy politician\Results\"
global data "C:\Users\saarimt1\Data\HST\journal"

*ssc install reghdfe
*ssc install eventstudyinteract

*********************************************************************************************
* Table 1: Political representation and socio-economic structure of neighborhoods (zipcode).
*********************************************************************************************

use "$data\zipcode_2012_estimation.dta", clear

la var stdincomepc "Standardized mean income"
la var educmscpc "Share highly-educated"

*analysis uses zipcodes which are not encrypted do to few observations (encrypted) and which are within one municipality (diffmun)
est clear
reghdfe candsha popsha stdincomepc if encrypted==0 & diffmun==0, absorb(kuntaidelec) cluster(kuntaidelec)
est sto inc1
reghdfe elecd popsha stdincomepc if encrypted==0 & diffmun==0, absorb(kuntaidelec) cluster(kuntaidelec)
est sto inc2
reghdfe elecsha popsha stdincomepc if encrypted==0 & diffmun==0, absorb(kuntaidelec) cluster(kuntaidelec)
est sto inc3
global tab "se(%9.3f) b(%9.3f) r2(%9.3f) nogaps star(* 0.05 ** 0.01) mtitles order(_cons) label"
esttab inc*, $tab keep(stdincomepc)

est clear
reghdfe candsha popsha educmscpc if encrypted==0 & diffmun==0, absorb(kuntaidelec) cluster(kuntaidelec)
est sto edu1
reghdfe elecd popsha educmscpc if encrypted==0 & diffmun==0, absorb(kuntaidelec) cluster(kuntaidelec)
est sto edu2
reghdfe elecsha popsha educmscpc if encrypted==0 & diffmun==0, absorb(kuntaidelec) cluster(kuntaidelec)
est sto edu3
global tab "se(%9.3f) b(%9.3f) nogaps star(* 0.05 ** 0.01) mtitles order(_cons) label"
esttab edu*, $tab keep(educmscpc)

*outcome means:
tabstat candsha elecd elecsha if encrypted==0 & diffmun==0 & zipnumb>1, stat(mean)


************************************************************************************
* Table 2. Candidate vote shares from own polling district.
*************************************************************************************

use "$data\EST_pollingdistrict_level_2012.dta", clear

la var d_oma_aa "Own district"
la var doma_restime "Own district x residence spell"
la var doma_inc "Own district x incumbent"
la var similarity "Voter similarity"
la var ownpartyvotesha "Own party vote share"

*analysis uses data from municipalities that have at least two polling districts (n_aanestysa)
est clear
reghdfe vsha_poldist100 d_oma_aa if n_aanestysa>1, absorb(hetu idaanialuefe) clu(kuntaidelec)
est sto reg1
reghdfe vsha_poldist100 d_oma_aa ownpartyvotesha similarity if n_aanestysa>1, absorb(hetu idaanialuefe) clu(kuntaidelec)
est sto reg2
reghdfe vsha_poldist100 d_oma_aa doma_restime doma_inc if n_aanestysa>1, absorb(hetu idaanialuefe) clu(kuntaidelec)
est sto reg3
reghdfe vsha_poldist100 d_oma_aa doma_restime doma_inc ownpartyvotesha similarity if n_aanestysa>1, absorb(hetu idaanialuefe) clu(kuntaidelec)
est sto reg4
global tab "se(%9.3f) b(%9.3f) r2(%9.2f) nogaps star(* 0.05 ** 0.01) mtitles order(_cons) label"
esttab reg1 reg2 reg3 reg4, $tab keep(d_oma_aa doma_restime doma_inc ownpartyvotesha similarity)

*outcome mean
tabstat vsha_poldist100 if n_aanestysa>1, stat(mean) 


*************************************************************************************
* Table 3: Effect of representation on school closure
*************************************************************************************

use "$data\causal_estimation_data.dta", clear

global ctrl_sch "pupils"
global ctrl_cand "age male child12 incumbent student unemployed entrepreneur highprofessional i.party"

la var elected "Elected"

*Panel A: Lottery
global cons "if nodup1==1"
est clear
reg closed_sch elected $cons, clu(kuntaidelec)
est sto reg1
reg closed_sch elected $ctrl_sch $cons, clu(kuntaidelec)
est sto reg2
reg closed_sch elected $ctrl_sch $ctrl_cand $cons, clu(kuntaidelec)
est sto reg3
reg closed_sch elected $ctrl_sch $ctrl_cand i.elecyear $cons, clu(kuntaidelec)
est sto reg4
global tab "se(%9.3f) b(%9.3f) r2(%9.3f) nogaps star(* 0.05 ** 0.01) mtitles order(_cons) label"
esttab reg1 reg2 reg3 reg4, $tab keep(_cons elected)

*Panel B: One vote margin
global cons "if nodup2==1"
est clear
reg closed_sch elected $cons, clu(kuntaidelec)
est sto reg1
reg closed_sch elected $ctrl_sch $cons, clu(kuntaidelec)
est sto reg2
reg closed_sch elected $ctrl_sch $ctrl_cand $cons, clu(kuntaidelec)
est sto reg3
reg closed_sch elected $ctrl_sch $ctrl_cand i.elecyear $cons, clu(kuntaidelec)
est sto reg4
global tab "se(%9.3f) b(%9.3f) r2(%9.3f) nogaps star(* 0.05 ** 0.01) mtitles order(_cons) label"
esttab reg1 reg2 reg3 reg4, $tab keep(_cons elected)

*P-value for effect difference
global cons1 "if nodup1==1"
global cons2 "if nodup2==1"

est clear
reg closed_sch elected $cons1
est sto reg1
reg closed_sch elected $cons2
est sto reg2
suest reg1 reg2, clu(kuntaidelec)
test [reg1_mean]elected-[reg2_mean]elected = 0

est clear
reg closed_sch elected $ctrl_sch $cons1
est sto reg1
reg closed_sch elected $ctrl_sch $cons2
est sto reg2
suest reg1 reg2, clu(kuntaidelec)
test [reg1_mean]elected-[reg2_mean]elected = 0

est clear
reg closed_sch elected $ctrl_sch $ctrl_cand $cons1
est sto reg1
reg closed_sch elected $ctrl_sch $ctrl_cand $cons2
est sto reg2
suest reg1 reg2, clu(kuntaidelec)
test [reg1_mean]elected-[reg2_mean]elected = 0

est clear
reg closed_sch elected $ctrl_sch $ctrl_cand i.elecyear $cons1
est sto reg1
reg closed_sch elected $ctrl_sch $ctrl_cand i.elecyear $cons2
est sto reg2
suest reg1 reg2, clu(kuntaidelec)
test [reg1_mean]elected-[reg2_mean]elected = 0


*************************************************************************************
* Figure 2: Effect of school closures on residential sorting, small schools.
*************************************************************************************

use "$data\zipcode_sorting.dta", clear

*nearest neighbor matching (nn = 2), get neighbors
nnmatch tunn closed_sch opp16 logpop if year==2013, tc(att) m(2) keep(nn_zip_schools_temp) replace
gen tunn_0=tunn

*check match data and create weights
preserve
use nn_zip_schools_temp, clear
unique tunn
unique tunn_0
bysort tunn_0: egen weight=count(tunn_0)
sort tunn_0
save nn_zip_schools, replace
restore

*merge neighbors
*nn_zip_schools includes only treated schools and their nearest neighbors
*neighbors' school id's are in tunn_0, thus merging with tunn_0 and keeping the ones that merge keeps
*only the schools that were chosen as nearest neighbors using nnmatch
merge m:m tunn_0 using nn_zip_schools.dta

*keep treated and nn controls
gen control=(_merge==3)
gen estsample=(closed_sch==1 | control==1)
keep if estsample==1

replace weight=1 if missing(weight)
expand weight

gen never_closed=(closeyear==.)

la var _Ttimetotre_1 "t = -3"
la var _Ttimetotre_2 "t = -2"
la var _Ttimetotre_4 "t = 0"
la var _Ttimetotre_5 "t = 1"
la var _Ttimetotre_6 "t = 2"
la var _Ttimetotre_7 "t = 3"

foreach var of varlist log_1_2 log_3_8 log_9_10 logpop {

eventstudyinteract `var' _Ttimetotre_* if schoolnumb==1 & opp16<90, cohort(closeyear) control_cohort(never_closed) absorb(i.zipcode i.year) vce(cluster zipcode)

	preserve
	matrix C = e(b_iw)
	matrix list C

	mata st_matrix("A",sqrt(st_matrix("e(V_iw)")))
	matrix C = C \ A
	matrix list C
	mat event = C'
	matrix list event
	svmat event

	keep event*
	replace event1 = 0 in 7
	replace event2 = 0 in 7
	drop if missing(event1)

	gen temp=_n
	gen x=0
	replace x=-3 if temp==1
	replace x=-2 if temp==2
	replace x=-1 if temp==7
	replace x=0 if temp==3
	replace x=1 if temp==4
	replace x=2 if temp==5
	replace x=3 if temp==6
	sort x
	rename event1 coef
	rename event2 se
	mkmat coef se, matrix(X)
	matrix Y`var' = X'

	restore
}

matrix list Ylog_1_2
matrix list Ylog_3_8
matrix list Ylog_9_10
matrix list Ylogpop

coefplot matrix(Ylogpop[1]), se(Ylogpop[2]) vertical xtitle("Years to closure") xline(3, lcolor(red)) yline(0, lcolor(red)) ylabel(-0.08(0.04)0.08) ytitle("") graphregion(color(white)) coeflabels(r1 = "-3" r2 = "-2" r3 = "-1" r4 = "0" r5 ="1" r6 ="2" r7 ="3") mcolor(black) ciopts(lcolor(black)) subtitle(Log population)
graph save "$results\logpop.gph", replace

coefplot matrix(Ylog_1_2[1]), se(Ylog_1_2[2]) vertical xtitle("Years to closure") xline(3, lcolor(red)) yline(0, lcolor(red)) ylabel(-0.08(0.04)0.08) ytitle("") graphregion(color(white)) coeflabels(r1 = "-3" r2 = "-2" r3 = "-1" r4 = "0" r5 ="1" r6 ="2" r7 ="3") mcolor(black) ciopts(lcolor(black)) subtitle(Log deciles 1-2)
graph save "$results\log_1_2.gph", replace

coefplot matrix(Ylog_3_8[1]), se(Ylog_3_8[2]) vertical xtitle("Years to closure") xline(3, lcolor(red)) yline(0, lcolor(red)) ylabel(-0.08(0.04)0.08) ytitle("") graphregion(color(white)) coeflabels(r1 = "-3" r2 = "-2" r3 = "-1" r4 = "0" r5 ="1" r6 ="2" r7 ="3") mcolor(black) ciopts(lcolor(black)) subtitle(Log deciles 3-8)
graph save "$results\log_3_8.gph", replace

coefplot matrix(Ylog_9_10[1]), se(Ylog_9_10[2]) vertical xtitle("Years to closure") xline(3, lcolor(red)) yline(0, lcolor(red)) ylabel(-0.08(0.04)0.08) ytitle("") graphregion(color(white)) coeflabels(r1 = "-3" r2 = "-2" r3 = "-1" r4 = "0" r5 ="1" r6 ="2" r7 ="3") mcolor(black) ciopts(lcolor(black)) subtitle(Log deciles 9-10)
graph save "$results\log_9_10.gph", replace

graph combine "$results\logpop.gph" "$results\log_1_2.gph" "$results\log_3_8.gph" "$results\log_9_10.gph", graphregion(color(white)) xsize(5) ysize(5)
*graph export "$results\small_school.png", replace
graph export "$results\small_school.eps", replace


*************************************************************************************
* Figure 5: Effect of school closures on residential sorting, large schools.
*************************************************************************************

use "$data\zipcode_sorting.dta", clear

*nearest neighbor matching (nn = 2), get neighbors
nnmatch tunn closed_sch opp16 logpop if year==2013, tc(att) m(2) keep(nn_zip_schools_temp) replace
gen tunn_0=tunn

*check match data and create weights
preserve
use nn_zip_schools_temp, clear
unique tunn
unique tunn_0
bysort tunn_0: egen weight=count(tunn_0)
sort tunn_0
save nn_zip_schools, replace
restore

*merge neighbors
*nn_zip_schools includes only treated schools and their nearest neighbors
*neighbors' school id's are in tunn_0, thus merging with tunn_0 and keeping the ones that merge keeps
*only the schools that were chosen as nearest neighbors using nnmatch
merge m:m tunn_0 using nn_zip_schools.dta

*keep treated and nn controls
gen control=(_merge==3)
gen estsample=(closed_sch==1 | control==1)
keep if estsample==1

replace weight=1 if missing(weight)
expand weight

gen never_closed=(closeyear==.)

la var _Ttimetotre_1 "t = -3"
la var _Ttimetotre_2 "t = -2"
la var _Ttimetotre_4 "t = 0"
la var _Ttimetotre_5 "t = 1"
la var _Ttimetotre_6 "t = 2"
la var _Ttimetotre_7 "t = 3"

foreach var of varlist log_1_2 log_3_8 log_9_10 logpop {

	eventstudyinteract `var' _Ttimetotre_* if schoolnumb==1 & opp16>=90, cohort(closeyear) 	control_cohort(never_closed) absorb(i.zipcode i.year) vce(cluster zipcode)

	preserve
	matrix C = e(b_iw)
	matrix list C

	mata st_matrix("A",sqrt(st_matrix("e(V_iw)")))
	matrix C = C \ A
	matrix list C
	mat event = C'
	matrix list event
	svmat event

	keep event*
	replace event1 = 0 in 7
	replace event2 = 0 in 7
	drop if missing(event1)

	gen temp=_n
	gen x=0
	replace x=-3 if temp==1
	replace x=-2 if temp==2
	replace x=-1 if temp==7
	replace x=0 if temp==3
	replace x=1 if temp==4
	replace x=2 if temp==5
	replace x=3 if temp==6
	sort x
	rename event1 coef
	rename event2 se
	mkmat coef se, matrix(X)
	matrix Y`var' = X'

	restore
}

matrix list Ylog_1_2
matrix list Ylog_3_8
matrix list Ylog_9_10
matrix list Ylogpop

coefplot matrix(Ylogpop[1]), se(Ylogpop[2]) vertical xtitle("Years to closure") xline(3, lcolor(red)) yline(0, lcolor(red)) ylabel(-0.08(0.04)0.08) ytitle("") graphregion(color(white)) coeflabels(r1 = "-3" r2 = "-2" r3 = "-1" r4 = "0" r5 ="1" r6 ="2" r7 ="3") mcolor(black) ciopts(lcolor(black)) subtitle(Log population)
graph save "$results\logpop.gph", replace

coefplot matrix(Ylog_1_2[1]), se(Ylog_1_2[2]) vertical xtitle("Years to closure") xline(3, lcolor(red)) yline(0, lcolor(red)) ylabel(-0.08(0.04)0.08) ytitle("") graphregion(color(white)) coeflabels(r1 = "-3" r2 = "-2" r3 = "-1" r4 = "0" r5 ="1" r6 ="2" r7 ="3") mcolor(black) ciopts(lcolor(black)) subtitle(Log deciles 1-2)
graph save "$results\log_1_2.gph", replace

coefplot matrix(Ylog_3_8[1]), se(Ylog_3_8[2]) vertical xtitle("Years to closure") xline(3, lcolor(red)) yline(0, lcolor(red)) ylabel(-0.08(0.04)0.08) ytitle("") graphregion(color(white)) coeflabels(r1 = "-3" r2 = "-2" r3 = "-1" r4 = "0" r5 ="1" r6 ="2" r7 ="3") mcolor(black) ciopts(lcolor(black)) subtitle(Log deciles 3-8)
graph save "$results\log_3_8.gph", replace

coefplot matrix(Ylog_9_10[1]), se(Ylog_9_10[2]) vertical xtitle("Years to closure") xline(3, lcolor(red)) yline(0, lcolor(red)) ylabel(-0.08(0.04)0.08) ytitle("") graphregion(color(white)) coeflabels(r1 = "-3" r2 = "-2" r3 = "-1" r4 = "0" r5 ="1" r6 ="2" r7 ="3") mcolor(black) ciopts(lcolor(black)) subtitle(Log deciles 9-10)
graph save "$results\log_9_10.gph", replace

graph combine "$results\logpop.gph" "$results\log_1_2.gph" "$results\log_3_8.gph" "$results\log_9_10.gph", graphregion(color(white)) xsize(5) ysize(5)
*graph export "$results\large_school.png", replace
graph export "$results\large_school.eps", replace


